class IUP_PROGRESS_BAR
-- Creates a progress bar control. Shows a percent value that can be updated to 
-- simulate a progression.

inherit
	IUP_WIDGET
		redefine
			execute_map,
			execute_unmap,
			execute_destroy
		end
	IUP_WIDGET_BGCOLOR
	IUP_WIDGET_FGCOLOR
	IUP_WIDGET_RASTERSIZE
	IUP_WIDGET_USERSIZE
	IUP_WIDGET_ACTIVE
	IUP_WIDGET_EXPAND
	IUP_WIDGET_FONT
	IUP_WIDGET_SCREENPOSITION
	IUP_WIDGET_POSITION
	IUP_WIDGET_MAXMIN_SIZE
	IUP_WIDGET_TIP
	IUP_WIDGET_SIZE
	IUP_WIDGET_ZORDER
	IUP_WIDGET_VISIBLE
	IUP_WIDGET_CHILD
	IUP_WIDGET_NAME
	IUP_WIDGET_CUSTOM_ATTRIBUTES

create {ANY}
	progress_bar
	
feature {ANY}

	progress_bar
		-- Create a new progress bar.
		local
			a_progress_bar: POINTER
		do
			min := 0
			max := 1
			
			a_progress_bar := int_progress_bar
			set_widget(a_progress_bar)
		end

	-- Attributes

	set_dashed (state: BOOLEAN)
		-- (creation only in Windows) [Windows and GTK only]: Changes the style 
		-- of the progress bar for a dashed pattern. Default is "False".
		do
			iup_open.set_attribute(Current, "DASHED", boolean_to_yesno(state))
		end

	set_marquee (state: BOOLEAN)
		-- (creation): displays an undefined state. Default: False. You can set 
		-- the attribute after map but only to start or stop the animation. In 
		-- Windows it will work only if using Visual Styles.
		do
			iup_open.set_attribute(Current, "MARQUEE", boolean_to_yesno(state))
		end

	set_max (value: REAL_64)
		-- (non inheritable): Contains the maximum value. Default is "1". The 
		-- control display is not updated, must set VALUE attribute to 
		-- update.
		require
			value > min
		do
			iup_open.set_attribute(Current, "MAX", value.to_string)

			-- Update value
			max := value
		ensure
			updated: max = value
		end

	set_min (value: REAL_64)
		-- (non inheritable): Contains the minimum value. Default is "0". The 
		-- control display is not updated, must set VALUE attribute to 
		-- update.
		require
			value < max
		do
			iup_open.set_attribute(Current, "MIN", value.to_string)

			-- Update value
			min := value
		ensure
			updated: min = value
		end

	set_horizontal_orientation
		-- (creation only): Set an horizontal orientation (from left to 
		-- right). This is the default value.
		do
			iup_open.set_attribute(Current, "ORIENTATION", "HORIZONTAL")
		end

	set_vertical_orientation
		-- (creation only): Set a vertical orientation (from bottom to 
		-- top). By default the progress bar has an horizontal 
		-- orientation (from left to right).
		do
			iup_open.set_attribute(Current, "ORIENTATION", "VERTICAL")
		end

	set_value (value: REAL_64)
		-- (non inheritable): Contains a number between "MIN" and "MAX", 
		-- controlling the current position.
		do
			iup_open.set_attribute(Current, "VALUE", value.to_string)
		end

	get_value: REAL_64
		-- (non inheritable): Contains a number between "MIN" and "MAX", 
			-- controlling the current position.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "VALUE")

			if str.is_real then
				Result := str.to_real
			end
		end

	-- Callbacks
	set_cb_map (act: FUNCTION[TUPLE[IUP_PROGRESS_BAR], STRING])
		-- Called right after an element is mapped and its attributes updated.
		local
			operation: INTEGER
		do
			cb_map := act
			
			if cb_map /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
		end

	set_cb_unmap (act: FUNCTION[TUPLE[IUP_PROGRESS_BAR], STRING])
		-- Called right before an element is unmapped.
		local
			operation: INTEGER
		do
			cb_unmap := act

			if cb_unmap /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
		end

	set_cb_destroy (act: FUNCTION[TUPLE[IUP_PROGRESS_BAR], STRING])
		-- Called right before an element is destroyed.
		local
			operation: INTEGER
		do
			cb_destroy := act

			if cb_destroy /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
		end

feature {IUP} -- Internal handle of callbacks

	execute_map: STRING
		do
			Result := cb_map.item([Current])
		end

	execute_unmap: STRING
		do
			Result := cb_unmap.item([Current])
		end

	execute_destroy: STRING
		do
			Result := cb_destroy.item([Current])
		end

feature {}

	-- For limits
	min: REAL_64
	max: REAL_64

	-- For callbacks
	cb_map: FUNCTION[TUPLE[IUP_PROGRESS_BAR], STRING]
	cb_unmap: FUNCTION[TUPLE[IUP_PROGRESS_BAR], STRING]
	cb_destroy: FUNCTION[TUPLE[IUP_PROGRESS_BAR], STRING]

	-- Internals

	int_progress_bar: POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupProgressBar()"
         }"
      end

invariant

	max > min

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
